package lolxxs.chenyanting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lolxxs.chenyanting.mapper.LogMapper;
import lolxxs.chenyanting.mapper.ResumeInformationMapper;
import lolxxs.chenyanting.mapper.UserMapper;
import lolxxs.chenyanting.model.Log;
import lolxxs.chenyanting.model.ResumeInformation;
import lolxxs.chenyanting.model.Statistics;
import lolxxs.chenyanting.mapper.StatisticsMapper;
import lolxxs.chenyanting.model.User;
import lolxxs.chenyanting.service.ILogService;
import lolxxs.chenyanting.service.IResumeInformationService;
import lolxxs.chenyanting.service.IStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lolxxs.chenyanting.service.IUserService;
import lolxxs.chenyanting.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2021-12-30
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, Statistics> implements IStatisticsService {

    @Autowired
    @SuppressWarnings("all")
    //all to suppress all warnings （抑制所有警告）
    // 由于编译器无法检测到mybtis-plus里面的创建bean代码会出现警告
    private StatisticsMapper statisticsMapper;

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private ResumeInformationMapper resumeInformationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    @Qualifier(value = "userServiceImpl")
    private IUserService userService;

    @Autowired
    @Qualifier(value = "logServiceImpl")
    private ILogService logService;

    @Autowired
    @Qualifier(value = "resumeInformationServiceImpl")
    private IResumeInformationService resumeInformationService;


    @Autowired
    private IdWorker idWorker;


    @Override
    public List<Statistics> query(Statistics statistics) {
        List<Statistics> statistices = statisticsMapper.selectList(queryWrapper(statistics));
        if (statistices.size() > 0) {
            return statistices;
        }
        return null;
    }

    private QueryWrapper<Statistics> queryWrapper(Statistics statistics) {
        QueryWrapper<Statistics> wrapper=new QueryWrapper<>();
        if (statistics.getStatisticsId() != null) {
            wrapper.eq("statisticsId", statistics.getStatisticsId());
        }
        if (statistics.getRegisterNumber() != null) {
            //大于
            wrapper.ge("registerNumber", statistics.getRegisterNumber());
        }
        if (statistics.getVisitNumber() != null) {
            //大于
            wrapper.ge("visitNumber", statistics.getVisitNumber());
        }
        if (statistics.getResumeNumber() != null) {
            wrapper.ge("resumeNumber", statistics.getResumeNumber());
        }
        if (statistics.getCreateTime() != null) {
            // 大于左边时间
            wrapper.ge("createTime", statistics.getCreateTime());
        }
        if (statistics.getUpdateTime() != null) {
            // 小于右边时间
            wrapper.le("createTime", statistics.getUpdateTime());
        }
        return wrapper;
    }
    // 秒 分 时 日 月 星期
    // 每个30秒执行一次
//    @Scheduled(cron = "*/15 * * * * ?")
    public void execute() {
        Date date = new Date();
        Date date1 = initDateByDay();//当天0时0分0秒
        Date date2 = latelyDateByDay();//当前23时59分59秒
        Map<String, Object> map = new HashMap<>();
        map.put("createTime", date1);

        List<Statistics> statistics1 = statisticsMapper.selectByMap(map);
        System.out.println(date1);
        System.out.println(statistics1.size());
        if (statistics1.size() > 0) {
            Statistics statistics = statistics1.get(0);
            User user = new User();//查询user表今日今日注册量
            user.setCreateTime(date1);
            user.setUpdateTime(date2);
            List<User> userQuery = userService.query(user);
            if (userQuery == null) {
                statistics.setRegisterNumber((long)0);
            } else {
                statistics.setRegisterNumber((long)userQuery.size());
            }


            Log log = new Log(); // 查询log表今日进入访问量
            log.setCreateTime(date1);
            log.setUpdateTime(date2);
            List<Log> logQuery = logService.query(log);
            if (logQuery == null) {
                statistics.setVisitNumber((long)0);
            } else {
                statistics.setVisitNumber((long)logQuery.size());
            }


            //查询ResumeInformation 表获取今日投递简历量
            ResumeInformation resumeInformation = new ResumeInformation();
            log.setCreateTime(date1);
            log.setUpdateTime(date2);
            List<ResumeInformation> resumeInformationQuery = resumeInformationService.query(resumeInformation);
            if (resumeInformationQuery == null) {
                statistics.setResumeNumber((long)0);
            } else {
                statistics.setResumeNumber((long)resumeInformationQuery.size());
            }


            statistics.setUpdateTime(date);
            statisticsMapper.updateById(statistics);
        } else {
            Statistics statistics = new Statistics();
            statistics.setStatisticsId(idWorker.nextId());

            User user = new User();//查询user表获取今日注册量
            user.setCreateTime(date1);
            user.setUpdateTime(date2);
            List<User> userQuery = userService.query(user);
            if (userQuery == null) {
                statistics.setRegisterNumber((long)0);
            } else {
                statistics.setRegisterNumber((long)userQuery.size());
            }


            Log log = new Log(); // 查询log表今日进入访问量
            log.setCreateTime(date1);
            log.setUpdateTime(date2);
            List<Log> logQuery = logService.query(log);
            if (logQuery == null) {
                statistics.setVisitNumber((long)0);
            } else {
                statistics.setVisitNumber((long)logQuery.size());
            }

            //查询ResumeInformation 表获取今日投递简历量
            ResumeInformation resumeInformation = new ResumeInformation();
            log.setCreateTime(date1);
            log.setUpdateTime(date2);
            List<ResumeInformation> resumeInformationQuery = resumeInformationService.query(resumeInformation);
            if (resumeInformationQuery == null) {
                statistics.setResumeNumber((long)0);
            } else {
                statistics.setResumeNumber((long)resumeInformationQuery.size());
            }




            statistics.setCreateTime(date1);
            statistics.setUpdateTime(date);
            statisticsMapper.insert(statistics);
        }
    }



    /**
     * 获得当天零时零分零秒
     * @return
     */
    public static Date initDateByDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);//HOUR_OF_DAY 24小时制
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得明天0时0分0秒0毫秒
     * @return
     */
    public static Date latelyDateByDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天，
        calendar.set(Calendar.HOUR_OF_DAY, 0);//HOUR_OF_DAY 24小时制
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}
